package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.CustomException;
import com.example.common.Result;
import com.example.entity.Role;
import com.example.entity.UserRole;
import com.example.service.RoleService;
import com.example.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = {"角色接口列表"})
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private UserRoleService userRoleService;

    /**
     * 分页查询所有权限等级
     *
     * @param page
     * @param pageSize
     * @param roleName
     * @return
     */

    @ApiOperation("权限查询")
    @GetMapping("/list")
    public Result<Map<String, Object>> getRoleList(int page, int pageSize, String roleName) {
        Page page1 = new Page(page, pageSize);

        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(roleName), Role::getRoleName, roleName);

        roleService.page(page1, wrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("total", page1.getTotal());
        map.put("rows", page1.getRecords());
        return Result.success(map);

    }

    /**
     * 新增角色
     *
     * @param role
     * @return
     */
    @ApiOperation("新增权限")
    @PostMapping
    public Result<?> addRole(@RequestBody Role role) {
        roleService.addRole(role);
        return Result.success("新增角色成功");
    }

    /**
     * 修改角色
     *
     * @param role
     * @return
     */
    @ApiOperation("修改权限")
    @PutMapping
    public Result<?> updateRole(@RequestBody Role role) {
        roleService.updateRole(role);
        return Result.success("修改角色成功");
    }

    /**
     * 返回到修改界面的查询角色
     *
     * @param id
     * @return
     */

    @ApiOperation("返回到修改界面的查询权限")
    @GetMapping("/{id}")
    public Result<Role> getRoleById(@PathVariable Long id) {
        return Result.success(roleService.getRoleById(id));
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @ApiOperation("删除权限")
    @DeleteMapping("/{id}")
    public Result<?> deleteRoleById(@PathVariable Long id) {

        LambdaQueryWrapper<UserRole> userRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userRoleLambdaQueryWrapper.eq(UserRole::getRoleId,id);
        long count = userRoleService.count(userRoleLambdaQueryWrapper);
        if (count > 0) {
            throw new CustomException("该权限已被使用，不能删除");
        } else {
            roleService.deleteRoleById(id);
            return Result.success("删除角色成功");
        }


    }

    /**
     * 下拉栏所需
     *
     * @return
     */

    @ApiOperation("权限在修改界面显示")
    @GetMapping("/all")
    public Result<List<Role>> getAllRole() {
        List<Role> list = roleService.list();
        return Result.success(list);
    }
}

